package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.PartiesInformation;
import com.jiantong.lawEnforcing.service.PartiesInformationService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 当事人信息
 * Created by mz03 on 2017/8/15.
 */
@Slf4j
@OpenBean
public class PartiesInformationApis {
    @RpcConsumer
    private PartiesInformationService partiesInformationService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.partiesInformation.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public PartiesInformation findById(Long id){
        Response<PartiesInformation> response = partiesInformationService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find partiesInformation read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param partiesInformation
     * @return
     */
    @OpenMethod(
            key = "create.partiesInformation",
            paramNames = {"partiesInformation"},
            httpMethods = RequestMethod.POST
    )
    public Long create(PartiesInformation partiesInformation){
        Response<Long> response = partiesInformationService.create(partiesInformation);
        if(!response.isSuccess()){
            log.error("failed to create partiesInformation read by partiesInformation = {}, cause : {}", partiesInformation, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.partiesInformation",
            paramNames = {"partiesInformation"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(PartiesInformation partiesInformation){
        if(partiesInformation.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = partiesInformationService.update(partiesInformation);
        if(!response.isSuccess()){
            log.error("failed to update partiesInformation");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update partiesInformation");
        }
        return response.getResult();
    }

}
